<?php
if (php_sapi_name () != 'cli' || ! empty ( $_SERVER ['REMOTE_ADDR'] )) {
	echo "Access denied";
	
	exit ();
}

define ( "SITE_PATH", dirname ( __FILE__ ) );
// подключение ядра
include_once SITE_PATH . "/include/core.php";

set_time_limit ( 0 );

// очистка БД
$_SELECT = $_MYSQL->SELECT ( "SELECT `car-id` FROM `{p}cars` WHERE `car-date_add` < DATE_SUB('" . date ( "Y-m-d H:i:s" ) . "', INTERVAL " . $_Conf->site_lifetime . " DAY);", MYSQL_SQUERY );
$i = 0;
while ( $id = $_MYSQL->FETCH_Array ( $_SELECT ) ) {
	if ($i == 100) {
		Delete ( $del_IDS );
		$i = 0;
		$del_IDS = "";
	}
	$del_IDS .= ($del_IDS != "") ? (", " . $id ["car-id"]) : ($id ["car-id"]);
	$i ++;
}
if ($i > 0) {
	Delete ( $del_IDS );
}
$_SELECT = $_MYSQL->SELECT ( "SELECT DATE_SUB('" . date ( "Y-m-d" ) . "', INTERVAL " . $_Conf->log_lifetime . " DAY) as `date_del`;" );
unlink ( SITE_PATH . "/log_" . $_SELECT ["date_del"] . ".txt" );

function Delete($del_IDS) {
	global $_MYSQl;
	$_QUERY = $_MYSQL->QUERY ( "DELETE FROM `{p}cars` WHERE `car-id` IN (" . $del_IDS . ");" );
	$_QUERY = $_MYSQL->QUERY ( "DELETE FROM `{p}car-colors` WHERE `car-id` IN (" . $del_IDS . ");" );
	$_QUERY = $_MYSQL->QUERY ( "DELETE FROM `{p}car-decors` WHERE `car-id` IN (" . $del_IDS . ");" );
	$_QUERY = $_MYSQL->QUERY ( "DELETE FROM `{p}car-fuels` WHERE `car-id` IN (" . $del_IDS . ");" );
	$_QUERY = $_MYSQL->QUERY ( "DELETE FROM `{p}car-images` WHERE `car-id` IN (" . $del_IDS . ");" );
	$_QUERY = $_MYSQL->QUERY ( "DELETE FROM `{p}car-options` WHERE `car-id` IN (" . $del_IDS . ");" );
	$_QUERY = $_MYSQL->QUERY ( "DELETE FROM `{p}car-price` WHERE `car-id` IN (" . $del_IDS . ");" );
	$_QUERY = $_MYSQL->QUERY ( "DELETE FROM `{p}car-markmodel` WHERE `car-id` IN (" . $del_IDS . ");" );
	$_QUERY = $_MYSQL->QUERY ( "DELETE FROM `{p}car-dialer` WHERE `car-id` IN (" . $del_IDS . ");" );
	return 0;
}
?>
